#include "table.h"

Record::Record(int index):index_(index){}

Record::Record(){}

Record::~Record(){}
 
void Record::Show(){
    std::cout << std::setw(4) << "|" << std::setw(9) << "|"
            << std::setw(16) << "|" << std::setw(9)<< std::endl;
}

void Record::DecIndex(){
    index_--;
}

void Record::IncIndex(){
    index_++;
}

Student::Student(){}

Student::Student(int index):Record(index) {};

Student::Student(int index, std::string name, int id, double score):Record(index), name_(name), id_(id), score_(score){};

Student::~Student(){}
inline void Student::SetName(std::string name) {
        name_ = name;
}

inline void Student::SetId(int id) {
    id_ = id;
}

inline void Student::SetScore(double score){
    score_ = score;
}

double Student::GetScore(){
    return score_;
}

int Student::GetId(){
    return id_;
}

std::string Student::GetName(){
    return name_;
}

void Student::Show() {
    std::cout << "|" << std::setw(8) << index_
                << "|" << std::setw(8) << id_
                << "|" << std::setw(15) << name_
                << "|" << std::setw(8)  << score_
                << "|" << std::endl;
    std::cout << "-------------------------------------------" << std::endl;
}

Table::Table():Record(0){};
Table::~Table(){};

void Table::Show() {
    std::cout << "-------------------------------------------" << std::endl;
    ShowHeader();
    for(auto p = students_.begin(); p != students_.end(); ++p){
        p->Show();
    }
}

void Table::ShowHeader(){
    std::cout << "|" << std::setw(8) << std::internal<< "Index"
                << "|" << std::setw(8) << std::internal << "ID"
                << "|" << std::setw(15) << std::internal << "Name"
                << "|" << std::setw(8) << std::internal << "Score"
                << "|" << std::endl;
    std::cout << "-------------------------------------------" << std::endl;
}

void Table::ShowLine(int index){
    
    students_.at(index).Show();
}

bool Table::AddStu(Student &stud){
    last++;
    try{
        students_.push_back(stud);
    }catch(...){
        return false;
    }
    return true;
}

bool Table::MoveStu(int std){
    auto p = students_.begin() + std;
    students_.erase(p);
    while (p!=students_.end())
    {
        p->DecIndex();
    }
    last--;
    return true;
}

Student Table::FindStu(int index){
    return students_.at(index);
}

bool Table::ModifyStu(int index, Student& student){
    students_.at(index).SetId(student.GetId());
    students_.at(index).SetName(student.GetName());
    students_.at(index).SetScore(student.GetScore());
    return true;
}

bool Table::NewStu(){
    std::string name;
    int id;
    double score;
    std::cout << "New student name : ";
    std::cin >> name;
    std::cout << "\nID : ";
    std::cin >> id;
    std::cout << "\nscore : ";
    std::cin >> score;
    std::cout << std::endl;
    Student stu(last,  name, id, score);
    
    AddStu(stu);
    return true;

}